package com.example.studyapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.VolleyError
import com.example.studyapp.data.jsonInteraction
import com.example.studyapp.data.myRecyclerAdapter1
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject

//错题集
class errors : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getSupportActionBar()!=null){//去掉标题
            getSupportActionBar()?.hide()
        };
        setContentView(R.layout.activity_errors)
        val errors_ret: Button =this.findViewById(R.id.errors_ret)
        //val errors_title:mytitle =this.findViewById(R.id.errors_title)
        errors_ret.setOnClickListener {
            this.finish()
        }
        //首先获取错题集列表
        //val jel=jstruct_errors_list_send(username = "admin")
        val jsonobj= JSONObject("{\"dataclass\":\"errors_list\"}")
        object : jsonInteraction(jsonobj){
            override fun myResposeListener(response: JSONObject) {
                init_list(response)
            }
        }
    }
    //为界面填充信息
    fun init_list(strjson: JSONObject){
        val mydata:jstruct_errors_list
        //解析数据库
        try{
            mydata=Gson().fromJson(strjson.toString(), jstruct_errors_list::class.java)
        }catch (e: Exception){
            //Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT)
              //  .show()
            return
        }
        //首先判断error是否有错误
        if(mydata.error){
            Toast.makeText(this, "未能访问到数据", Toast.LENGTH_SHORT)
                .show()
            return
        }
        if(mydata.length<=0){
            Toast.makeText(this, "数据库没有数据", Toast.LENGTH_SHORT)
                .show()
            return
        }
        val layoutManager= LinearLayoutManager(this)
        val mre: RecyclerView =this.findViewById(R.id.errors_recyclerview)
        mre.layoutManager=layoutManager
        //将JSONArray转换为ArrayList
        val dblist=jsonToList<String>(mydata.listname.toString())
        val mylist=jsonToList<String>(mydata.timelist.toString())
        //设置选项布局
        val adapter=object : myRecyclerAdapter1(dblist,mylist){
            //设置选项单击事件
            override fun onItemClick(holder: MyViewHolder1, position: Int) {
                thisOnItemClick(holder,position,mydata)
            }
        }

        mre.adapter=adapter
    }
    //JsonArray转换成List
    private fun <T> jsonToList(jsonList: String): List<T> {
        return Gson().fromJson(jsonList, object : TypeToken<ArrayList<T>>() {}.type)
    }
    //点击选项，获取错题信息
    fun thisOnItemClick(
        holder: myRecyclerAdapter1.MyViewHolder1,
        index: Int,
        jsel: jstruct_errors_list
    ){
        //遍历错题列表，确定目前选中的
        val itemlist=jsonToList<Int>(jsel.items.toString())
        val paperlist=jsonToList<String>(jsel.listname.toString())
        //val jobj=JSONObject(Gson().toJson(jstruct_paper_item_send(item=itemlist[index],paper = paperlist[index],username = define_user_name)))
        val jsonj:String="{\"dataclass\":\"paper_item\",\"item\":"+itemlist[index]+",\"paper\":\""+paperlist[index]+"\",\"username\":\"admin\"";
        val jobj=JSONObject("{\"dataclass\":\"paper_item\"}")

        object : jsonInteraction(jobj){
            override fun myResposeListener(response: JSONObject) {
                //通过这里的信息加载新窗口
                open_item(response)
            }
        }
    }
    fun open_item(response: JSONObject):Boolean{
        val jspi:jstruct_paper_item=Gson().fromJson(response.toString(),jstruct_paper_item::class.java)
        if(jspi.error){
            return false
        }
        //判断题型
        if(jspi.type==myqu_type.cho.toString()){//选择题
            define_now_paper_info=jstruct_paper_info(papername = "",choice_qu_num = 0,completion_qu_num = 0,error = false)  //表示不是试题集
            define_now_paper_item=jspi
            val inte=Intent(this,study_choice_question_1::class.java)
            startActivity(inte)
        }
        return true
    }
}